﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AutoBackupDatabase.General;
using DevExpress.XtraEditors;
using AutoBackupDatabase.BLL;
using AutoBackupDatabase.ENT;

namespace AutoBackupDatabase
{
    public partial class DetailGroupUserRole_ucc : DevExpress.XtraEditors.XtraUserControl
    {
        private ROLEBLL roleBLL;
        private GROUPUSERENT groupUserENT;
        private GROUPUSERBLL groupUserBLL;
        private DETAILGROUPUSERROLEBLL detailGroupBLL;
        private DETAILGROUPUSERROLEENT detailGroupENT;

        public DetailGroupUserRole_ucc()
        {
            InitializeComponent();
            roleBLL = new ROLEBLL();
            groupUserBLL = new GROUPUSERBLL();
            detailGroupBLL = new DETAILGROUPUSERROLEBLL();
        }

        private void DetailGroupUserRole_ucc_Load(object sender, EventArgs e)
        {
            LoadRoleByGroupRoleCode();
            GetGroupUser();
        }

        private void GetGroupUser()
        {
            glkGroupUser.Properties.DataSource = groupUserBLL.GET_GROUPUSER_ALL();
            glkGroupUser.Properties.DisplayMember = "GROUPUSERNAME";
            glkGroupUser.Properties.ValueMember = "GROUPUSERCODE";
        }

        private void LoadRoleByGroupRoleCode()
        {
            try
            {
                groupUserENT = new GROUPUSERENT();
                groupUserENT.GROUPUSERCODE = int.Parse(glkGroupUser.EditValue.ToString());
                gridControl1.DataSource = roleBLL.GET_ROLE_PERMITSION(groupUserENT);
            }
            catch (Exception)
            {

                return;
            }

        }

        private void glkGroupUser_EditValueChanged(object sender, EventArgs e)
        {
            LoadRoleByGroupRoleCode();
        }

        private void btnSave_Click(object sender, EventArgs e){
            if (
                MessageBox.Show("Bạn có chắc là muốn lưu những thay đổi này", MessageShow.delConfirmCaption, MessageBoxButtons.YesNo,
                                MessageBoxIcon.Warning) == DialogResult.Yes){
                //delete all role
                detailGroupENT = new DETAILGROUPUSERROLEENT();
                detailGroupENT.GROUPUSERCODE = int.Parse(glkGroupUser.EditValue.ToString());
                detailGroupBLL.DELTELEDETAIL_GROUPUSER_ROLE(detailGroupENT);
                //insert role again
                for (int i = 0; i < gridView1.RowCount; i++){
                    if (gridView1.GetRowCellValue(i, "ISCHECK").ToString().ToLower() == "true")
                    {
                        detailGroupENT = new DETAILGROUPUSERROLEENT();
                        detailGroupENT.GROUPUSERCODE = int.Parse(glkGroupUser.EditValue.ToString());
                        detailGroupENT.ROLECODE = gridView1.GetRowCellValue(i, "ROLECODE").ToString();
                        detailGroupBLL.INSERTDETAILGROUPUSERROLE(detailGroupENT);
                    }
                }
                MessageBox.Show("Lưu thành công");LoadRoleByGroupRoleCode();
            }

        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            LoadRoleByGroupRoleCode();
        }
    }
}